package services;

import dao.Fhrb_Repository;
import models.ApplyInfo_T_Entity;
import models.DatabaseExecutionContext;
import models.Fhrb_T_Entity;
import play.db.jpa.JPAApi;
import vo.DataList;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class Fhrb_Repository_JPA implements Fhrb_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public Fhrb_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @Override
    public CompletionStage<DataList> detailList(String StartDate,String EndDate,String THDH)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
//                String strSql = "exec f_fhrb @StartDate = '" + StartDate + "',@EndDate = '"
//                        + EndDate + "', @THDH = '"
//                        + THDH + "'";
                String strSql = "select * from f_fhrb('" + StartDate + "','"+EndDate+"','"+THDH+"') ";
                List<Fhrb_T_Entity> list =
                        entityManager.createNativeQuery(strSql,Fhrb_T_Entity.class).getResultList();
                List<Map> listLog = new ArrayList<Map>();
                if(list.size() <= 0) {
                    Map<String, Object> fhrb = new HashMap<String, Object>();
                    fhrb.put("log", "无数据");
                    listLog.add(fhrb);
                    return new DataList(listLog);
                }
                return  new DataList(list);
            });
        }, executionContext);
    }

}
